package com.ww.springboot.boot.thread.study.base.lock.aqs.Demo1;

import java.util.concurrent.locks.Lock;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2023-02-13 10:16
 */
public class Main {

    //有竞争 才有锁
    private static Lock lock = new MyLock();

    public static void main(String[] args) throws InterruptedException {

        new Thread(() -> {
            try {
                lock.lockInterruptibly();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                System.out.println("线程1 获取锁!" + System.currentTimeMillis());
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        }).start();

        new Thread(() -> {
            lock.lock();
            try {
                System.out.println("线程2 获取锁!" + System.currentTimeMillis());
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();
    }
}
